from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.kq_log_data_vo import Kq_log_dataPageQueryModel, Kq_log_dataModel, DeleteKq_log_dataModel
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.kq_log_data_service import Kq_log_dataService
from module_admin.service.login_service import LoginService

from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil



kq_log_dataController = APIRouter(prefix='/kaoqin/kq_log_data')

@kq_log_dataController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_log_data:list'))]
)
async def get_kaoqin_kq_log_data_list(
    request: Request,
kq_log_data_page_query: Kq_log_dataPageQueryModel = Depends(Kq_log_dataPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    kq_log_data_page_query_result = await Kq_log_dataService.get_kq_log_data_list_services(query_db, kq_log_data_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=kq_log_data_page_query_result)

@kq_log_dataController.post('', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_log_data:add'))])
@ValidateFields(validate_model='add_kq_log_data')
@Log(title='考勤原始数据', business_type=BusinessType.INSERT)
async def add_kaoqin_kq_log_data(
    request: Request,
    add_kq_log_data: Kq_log_dataModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_kq_log_data.create_by = current_user.user.user_name
    add_kq_log_data.create_time = datetime.now()
    add_kq_log_data.update_by = current_user.user.user_name
    add_kq_log_data.update_time = datetime.now()
    add_kq_log_data_result = await Kq_log_dataService.add_kq_log_data_services(query_db, add_kq_log_data)
    logger.info(add_kq_log_data_result.message)

    return ResponseUtil.success(msg=add_kq_log_data_result.message)


@kq_log_dataController.put('', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_log_data:edit'))])
@ValidateFields(validate_model='edit_kq_log_data')
@Log(title='考勤原始数据', business_type=BusinessType.UPDATE)
async def edit_kaoqin_kq_log_data(
    request: Request,
    edit_kq_log_data: Kq_log_dataModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_kq_log_data.update_by = current_user.user.user_name
    edit_kq_log_data.update_time = datetime.now()
    edit_kq_log_data_result = await Kq_log_dataService.edit_kq_log_data_services(query_db, edit_kq_log_data)
    logger.info(edit_kq_log_data_result.message)

    return ResponseUtil.success(msg=edit_kq_log_data_result.message)


@kq_log_dataController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_log_data:remove'))])
@Log(title='考勤原始数据', business_type=BusinessType.DELETE)
async def delete_kaoqin_kq_log_data(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_kq_log_data = DeleteKq_log_dataModel(ids=ids)
    delete_kq_log_data_result = await Kq_log_dataService.delete_kq_log_data_services(query_db, delete_kq_log_data)
    logger.info(delete_kq_log_data_result.message)

    return ResponseUtil.success(msg=delete_kq_log_data_result.message)


@kq_log_dataController.get(
    '/{id}', response_model=Kq_log_dataModel, dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_log_data:query'))]
)
async def query_detail_kaoqin_kq_log_data(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    kq_log_data_detail_result = await Kq_log_dataService.kq_log_data_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=kq_log_data_detail_result)


@kq_log_dataController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('kaoqin:kq_log_data:export'))])
@Log(title='考勤原始数据', business_type=BusinessType.EXPORT)
async def export_kaoqin_kq_log_data_list(
    request: Request,
    kq_log_data_page_query: Kq_log_dataPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    kq_log_data_query_result = await Kq_log_dataService.get_kq_log_data_list_services(query_db, kq_log_data_page_query, is_page=False)
    kq_log_data_export_result = await Kq_log_dataService.export_kq_log_data_list_services(kq_log_data_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(kq_log_data_export_result))
